import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import '../../tool/dart_toast.dart';
import '../../widget/my_dialog.dart';
import 'dialog_show_page.dart';

class DialogPage extends StatefulWidget {
  const DialogPage({super.key});

  @override
  State<DialogPage> createState() => _DialogPageState();
}

class _DialogPageState extends State<DialogPage> {
  void _alertDialog() {
    showDialog(
      context: context,
      builder: (context) => AlertDialog(
        title: Text("AlertDialog"),
        content: Text("这是一个AlertDialog"),
        actions: [
          TextButton(
            onPressed: () {
              Navigator.of(context).pop();
            },
            child: Text("取消"),
          ),
          TextButton(
            onPressed: () {
              Navigator.of(context).pop();
            },
            child: Text("确定"),
          ),
        ],
      ),
    );
  }

  void _simpleDialog() async {
    print("_simpleDialog");
    var result = await showDialog(
      barrierDismissible: true,
      context: context,
      builder: (context) => SimpleDialog(
        alignment: Alignment.topRight,
        title: Text("请选择语言"),
        children: [
          SimpleDialogOption(
            child: Text("选项1"),
            onPressed: () {
              Navigator.of(context).pop("选项1");
            },
          ),
          Divider(),
          SimpleDialogOption(
            child: Text("选项2"),
            onPressed: () {
              Navigator.of(context).pop("选项2");
            },
          ),
        ],
      ),
    );
    print("对话框返回值: $result");
  }

  void _modelBottomDialog() async {
    print("_modelBottomDialog");
    var result = await showModalBottomSheet(
      context: context,
      builder: (context) => SizedBox(
        height: 280,
        child: Column(
          children: [
            ListTile(
              title: Text("选项1"),
              onTap: () {
                Navigator.of(context).pop("选项1");
              },
            ),
            ListTile(
              title: Text("选项2"),
              onTap: () {
                Navigator.of(context).pop("选项2");
              },
            ),
          ],
        ),
      ),
    );
    print("对话框返回值: $result");
  }

  void _toast() async {
    DartToast.showToast(
      context: context,
      msg: "这是一个Toast!!!!!!!!!!!!!!!",
      toastLength: DartToast.LENGTH_LONG,
      gravity: DartToast.BOTTOM,
      backgroundColor: Colors.black,
      textColor: Colors.white,
      fontSize: 20.0,
      borderRadius: 0.5,
      opacity: 0.2,
    );
  }

  void _myDialog() {
    showDialog(
      barrierDismissible: false,
      context: context,
      builder: (context) => MyDialog(
        title: "自定义提示",
        content: "这是一个自定义dialog",
        onTap: () {
          if (kDebugMode) {
            print("点击了关闭按钮");
          }
          Navigator.of(context).pop();
        },
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Dialog')),
      body: Center(
        child: Column(
          children: <Widget>[
            ElevatedButton(
              onPressed: _alertDialog,
              child: Text('_alertDialog'),
            ),
            ElevatedButton(
              onPressed: _simpleDialog,
              child: Text('_simpleDialog'),
            ),
            ElevatedButton(
              onPressed: _modelBottomDialog,
              child: Text('_modelBottomDialog'),
            ),
            ElevatedButton(onPressed: _toast, child: Text('_toast')),
            ElevatedButton(onPressed: _myDialog, child: Text('自定义dialog')),
            ElevatedButton(onPressed: (){
              //使用dialog打开页面dialog_show_page
              showDialog(
                context: context,
                builder: (BuildContext context) {
                  return DialogShowPage(
                    unlockFunction: () {
                      // 处理解锁逻辑
                      print("解锁成功");
                    },
                  );
                },
              );
            }, child: Text('dialog转递方法回调')),
          ],
        ),
      ),
    );
  }
}
